package com.borqsprocessmanager.receiver;

import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.BatteryManagerService;
import com.borqsprocessmanager.Constant;
import com.borqsprocessmanager.db.DBProvider.ActionTable;
import com.borqsprocessmanager.info.ReceiverProcessInfo;
import com.borqsprocessmanager.util.Log;

public class PackageChangedReceiver extends BroadcastReceiver {
	private final String TAG = "PackageReceiver";

	private BatteryManagerService mBatteryManagerService;
	private ReceiverProcessInfo info;
	private String removedAppPackgeName = "";

	public PackageChangedReceiver(BatteryManagerService batteryManagerService) {
		mBatteryManagerService = batteryManagerService;
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
			String addAppPackgeName = intent.getDataString();
			addAppPackgeName = addAppPackgeName.replace("package:", "");
			if (addAppPackgeName.equals(removedAppPackgeName)) {
				addPackagenameFormDB(info);
			}
		} else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
			if (!intent.getDataString().equals(Constant.APP_PKG)) {
				removedAppPackgeName = intent.getDataString();
				removedAppPackgeName = removedAppPackgeName.replace("package:",
						"");

				boolean isSavedDB = queryPackageNameFormDB(removedAppPackgeName);
				if (isSavedDB) {
					deleteInfoFormDB(removedAppPackgeName);
					mBatteryManagerService.inintAcitonMap();
				}
			}
		}
	}

	private void addPackagenameFormDB(ReceiverProcessInfo info) {
		if (info != null) {
			ContentValues values = new ContentValues();
			values.put(ActionTable.APPNAME, info.appName);
			values.put(ActionTable.PKG_NAME, info.packageName);
			values.put(ActionTable.PROCESS_NAME, info.processName);
			values.put(ActionTable.ACATION_TYPE, info.isClose);
			App.mContext.getContentResolver().insert(ActionTable.CONTENT_URI,
					values);
		}
	}

	private boolean queryPackageNameFormDB(String appName) {
		Cursor cursor = null;
		try {
			cursor = App.mContext.getContentResolver()
					.query(ActionTable.CONTENT_URI,
							new String[] { ActionTable.PROCESS_NAME },
							ActionTable.PKG_NAME + "=?",
							new String[] { appName }, null);
			if (cursor != null) {
				for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
						.moveToNext()) {
					info = new ReceiverProcessInfo();
					info.setAppName(cursor.getString(cursor
							.getColumnIndex(ActionTable.APPNAME)));
					info.setPackageName(cursor.getString(cursor
							.getColumnIndex(ActionTable.PKG_NAME)));
					info.setProcessName(cursor.getString(cursor
							.getColumnIndex(ActionTable.PROCESS_NAME)));
					info.setIsClose(cursor.getInt(cursor
							.getColumnIndex(ActionTable.ACATION_TYPE)));
					Log.v(TAG, "appName===" + info.appName);
				}
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return false;
	}

	private void deleteInfoFormDB(String appPackgeName) {
		App.mContext.getContentResolver().delete(ActionTable.CONTENT_URI,
				ActionTable.PKG_NAME + "=?", new String[] { appPackgeName });
	}
}
